<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class RoleController extends IndexController {

    public function add() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        if (IS_POST) {
            $model = D('Role');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('lst?p=' . $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        //取出所有权限
        $primodel = D('Privilege');
        $priDate = $primodel->getTree();
        $this->assign('priDate', $priDate);

        $this->display();
    }

    public function edit() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        if (IS_POST) {
            $model = D('Role');
            if ($model->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('lst', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Role');
        $data = $model->find($id);
        $this->assign('data', $data);

        $arolemodel = M('RolePrivilege');
        $aroleData = $arolemodel->field('group_concat(pri_id) pri_id')->where("role_id=$id")->find();
        $rid = $aroleData['pri_id'];
        $this->assign('rid', $rid);
        //取出所有权限
        $primodel = D('Privilege');
        $priDate = $primodel->getTree();
        $this->assign('priDate', $priDate);
        $this->display();
    }

    public function delete() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $model = D('Role');
        if ($model->delete(I('get.id', 0)) !== FALSE) {
            $this->success('删除成功！', U('lst', array('p' => $p)));
            exit;
        } else {
            $this->error($model->getError());
        }
    }

    public function lst() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $model = D('Role');
        $data = $model->search();
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));

        $this->display();
    }

}
